<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('base_new.css,destination_dialog_setdest.css,logistics.css'); }
</head>

<body style=" overflow: hidden">

<div class="s-main">
    <div class="destination-city-wrapper">
        <ul class="destination-city-box clearfix provicelist">
            {loop $tops $t}
            <li class="item">
                <label class="check-label provice"   title="{$t['cityname']}" >
                    <input class="provinceid" data-id="{$t['id']}" type="checkbox"  {if in_array($t['id'],$province_arr)}checked {/if}  name="provinceid[]" value="{$t['id']}" />{$t['shortname']}
                </label>
                <i class="num get-child" data-pid="{$t['id']}">{$t['childnum']}</i>
            </li>
            {/loop}
        </ul>
    </div>

    <div class="s-list hide" id="city_all">
        <div class="con-one all_menu">
            <a href="javascript:" id="all_select">全选</a>|<a href="javascript:" id="reverse_select">反选</a>
        </div>
    </div>
    <div class="destination-city-wrapper">
        <ul class="destination-city-box clearfix child-list">

        </ul>
    </div>
    <div class="clearfix text-c mt-20">
        <a class="btn btn-primary radius " href="javascript:;" id="confirm-btn">确定</a>
    </div>
    <input type="hidden" value="{$planid}" name="planid" id="planid">
    <input type="hidden" value="{$citylist}" name="citylist" id="citylist">

</div>

<script>
    $(function(){
        window.setTimeout(function(){
            ST.Util.resizeDialog('.s-main');
        },0);


        $('.provice').click(function () {
           $(this).next().trigger('click');
           return false;




        });


        //展示下级
        $('.get-child').click(function () {
            if($(this).hasClass('on'))
            {
                return true;
            }
            $('.destination-city-box .num').removeClass('on');
            $(this).addClass('on');
            var pid = $(this).data('pid');
            var childlist = get_child_list(pid);
            var html = '';
            var citylist = $('#citylist').val();
            if(citylist!='')
            {

                citylist = citylist.split(',');
            }
            else
            {
                citylist = [];
            }
            $(childlist).each(function (i,v) {

                var checked = '';
                if(citylist.indexOf(v.id)!=-1)
                {
                     checked = 'checked'
                }

                html += '<li class="item"><label class="check-label cityid"  title="'+v.cityname+'" data-pid="'+pid+'"  data-id="'+v.id+'">' +
                    '<input type="checkbox" '+checked+' >'+v.shortname+'</label></li>';

            });
            $('.child-list').html(html);
            $('.s-list').removeClass('hide');
            ST.Util.resizeDialog('.s-main');
        });
        //选中单个下级
        $('.child-list').on('change','.cityid input',function ()
        {
            var cityid = $(this).parent().data('id');
            var pid = $(this).parent().data('pid');
            if($(this).is(':checked'))
            {
                change_city(cityid,'add')

            }
            else
            {
                change_city(cityid,'del')
            }
            var hascity = $('.child-list .cityid input:checked').length;
            if(hascity>0)
            {
                $('.provinceid[data-id='+pid+']').attr("checked",true)
            }
            else
            {
                $('.provinceid[data-id='+pid+']').attr("checked",false)
            }
        });
        //全选
        $('#city_all #all_select').click(function () {
            $('.child-list .cityid input').attr('checked',true);
            $('.child-list .cityid input').each(function (i,v)
            {
                var cityid = $(v).parent().data('id');
                var pid = $(v).parent().data('pid');
                change_city(cityid,'add')
                var hascity = $('.child-list .cityid input:checked').length;
                if(hascity>0)
                {
                    $('.provinceid[data-id='+pid+']').attr("checked",true)
                }
                else
                {
                    $('.provinceid[data-id='+pid+']').attr("checked",false)
                }
            })
        });
        //反选
        $('#city_all #reverse_select').click(function () {
            $('.child-list .cityid input').each(function (i,v)
            {
                var cityid = $(v).parent().data('id');
                var pid = $(v).parent().data('pid');
                if($(this).is(':checked'))
                {
                    $(this).attr('checked',false);
                    change_city(cityid,'del')

                }
                else
                {
                    $(this).attr('checked',true);
                    change_city(cityid,'add')
                }
                var hascity = $('.child-list .cityid input:checked').length;
                if(hascity>0)
                {
                    $('.provinceid[data-id='+pid+']').attr("checked",true)
                }
                else
                {
                    $('.provinceid[data-id='+pid+']').attr("checked",false)
                }
            })
        });



        $(document).on('click','#confirm-btn',function(){
            var provinceids = [];
             $('.provinceid:checked').each(function () {
                 provinceids.push($(this).val())
             });

            provinceids = provinceids .join(',');
            var citylist = $('#citylist').val();
            var planid = $('#planid').val();

            ST.Util.responseDialog({provinceids:provinceids,citylist:citylist,planid:planid},true);
        })

    });

    //获取下级
    function get_child_list(pid) {
        var list = '';
        $.ajax({
            type:'post',
            dataType:'json',
            async:false,
            data:{pid:pid},
            url:SITEURL+'express/ajax_get_nextcity',
            success:function (data) {
                list = data.list;
            }
        });
        return list;

    }

    /**
     * @function 城市改变
     * @param cityid
     * @param provinceid
     * @param type
     */
    function change_city(cityid,type)
    {
        var cityid = String(cityid);
        var citylist = $('#citylist').val();
        if(citylist!='')
        {

            citylist = citylist.split(',');
        }
        else
        {
            citylist = [];
        }
        if(type=='add')
        {
            if($.inArray(cityid,citylist)==-1)
            {
                citylist.push(cityid);
            }
        }
        else if(type=='del')
        {
            var index = $.inArray(cityid,citylist);
           if(index!=-1)
           {
               citylist.splice(index,1);
           }

        }
        citylist = citylist.join(',');
        $('#citylist').val(citylist);

    }




</script>

</body>
</html>
